package educationaladministrationsystem.CSM.Controller.Root;

import educationaladministrationsystem.Class.EssayUpload;
import educationaladministrationsystem.Result.Result;
import educationaladministrationsystem.CSM.Mapper.Service.Root.RootMajorService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;

//主页数据传送层
@Slf4j
@RestController
@RequestMapping("/Root/Major")
public class RootMajorController {
    @Autowired
    private RootMajorService rootMajorService;

    //    获取可用公告号
    @GetMapping("/availableEssayId")
    public Result getAvailableEssayId() {
        Integer availableEssayId;
        try {
            availableEssayId = rootMajorService.getAvailableEssayId();
        } catch (Exception e) {
            availableEssayId = 1;
        }
        return Result.success(availableEssayId);
    }

    //    上传文章
    @PostMapping("/upload")
    public Result uploadEssay(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
//        根据字段获取数据
        int id = Integer.parseInt(request.getParameter("id"));
        String title = request.getParameter("title");
        MultipartFile image = multipartHttpServletRequest.getFile("image");
        String textInformation = request.getParameter("textInformation");
        String date = request.getParameter("date");
//        传送到处理层进行编码
        return Result.success(rootMajorService.saveEssay(new EssayUpload(id, title, image, textInformation, date)));
    }

    //        下载文章
    @GetMapping("/download")
    public Result downloadEssay(@RequestParam Integer page, @RequestParam Integer pageNumber) {
        return Result.success(rootMajorService.downloadEssay(page, pageNumber));
    }

    //    删除文章
    @PostMapping("/deleteEssay")
    public Result deleteEssay(@RequestParam Integer deleteId) {
        try {
            rootMajorService.deleteEssay(deleteId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }

    //        获取最大页数
    @GetMapping("/allPage")
    public Result getAllPage(@RequestParam Integer pageNumber) {
        return Result.success(rootMajorService.getAllPage(pageNumber));
    }

    @GetMapping("/getNoneDownloadEssay")
    public Result getOneDownloadEssay(@RequestParam Integer id) {
        return Result.success(rootMajorService.getOneDownloadEssay(id));
    }

    @PostMapping("/updateEssay")
    public Result updateEssay(@RequestBody EssayUpload essayUpload) {
        try {
            rootMajorService.updateEssay(essayUpload);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }
}
